// npm i axios
const {default: Axios} = require('axios');

// npm install nodemailer  #安装邮件发送模块
const nodemailer = require('nodemailer');

// npm install node-schedule  安装定时器
const schedule = require('node-schedule');

function getHoneyedWords() {
  //https://pyq.shadiao.app/api.php 朋友圈
  //https://nmsl.shadiao.app/api.php?level=min&lang=zh_cn   骂人
  //https://chp.shadiao.app/api.php   情话
  //https://du.shadiao.app/api.php   毒鸡汤
  var url = 'https://chp.shadiao.app/api.php';
  //获取这个接口的信息
  return Axios.get(url);
}

// https://juejin.cn/post/6904805497845579783


// 发送邮件函数
async function sendMail(text, to) {
  var user = '253399414@qq.com';//自己的邮箱
  var pass = 'xyrecyxctkdbbiac'; //qq邮箱授权码
  let transporter = nodemailer.createTransport({
    host: 'smtp.qq.com',
    port: 587,
    secure: false,
    auth: {
      user: user, // 用户账号
      pass: pass //授权码,通过QQ获取
    }
  });
  let info = await transporter.sendMail({
    from: `李伟峰<${user}>`, // sender address
    to: `陈兰兰<${to}>`, // list of receivers
    subject: '亲爱的陈大兰', // Subject line
    text: text // plain text body
  });
  console.log('发送成功');
}

//测试一下
// getHoneyedWords().then(res => {
//   console.log(res.data);
//   let to1 = '295587683@qq.com';
//   // 发送邮件
//   sendMail(res.data, to1);
// });


//每天早上5点21分发送
schedule.scheduleJob({hour: 5, minute: 21}, function () {
  console.log('启动任务:' + new Date());
  getHoneyedWords().then((res) => {
    console.log(res.data);
    let to1 = '1258073422@qq.com';
    let to2 = '416916279@qq.com';
    sendMail(res.data, to1).then(() => {
      console.log('发送成功');
    });
    sendMail(res.data, to2).then(() => {
      console.log('发送成功');
    });
  });
});

//每天下午13：14
schedule.scheduleJob({hour: 13, minute: 14}, function () {
  console.log('启动任务:' + new Date());
  getHoneyedWords().then((res) => {
    console.log(res.data);
    let to1 = '1258073422@qq.com';
    let to2 = '416916279@qq.com';
    sendMail(res.data, to1).then(() => {
      console.log('发送成功');
    });
    sendMail(res.data, to2).then(() => {
      console.log('发送成功');
    });
  });
});

//每天下午17：21
schedule.scheduleJob({hour: 17, minute: 21}, function () {
  console.log('启动任务:' + new Date());
  getHoneyedWords().then((res) => {
    console.log(res.data);
    let to1 = '1258073422@qq.com';
    let to2 = '416916279@qq.com';
    sendMail(res.data, to1).then(() => {
      console.log('发送成功');
    });
    sendMail(res.data, to2).then(() => {
      console.log('发送成功');
    });
  });
});
